import { Inject, Post, Query } from '@midwayjs/core';
import { CoolController, BaseController } from '@cool-midway/core';
import { MallSpecEntity } from '../../entity/spec';
import { MallSpecService } from '../../service/spec';

/**
 * 商品规格
 */
@CoolController({
  api: ['add', 'delete', 'update', 'info', 'list', 'page'],
  entity: MallSpecEntity,
  service: MallSpecService,
  pageQueryOp: {
    keyWordLikeFields: ['name'],
    fieldEq: ['goodsId'],
    where: async ctx => {
      const { stockThreshold } = ctx.request.body;
      if (stockThreshold) {
        return [['a.stock <= :stockThreshold', { stockThreshold }]];
      }
    },
  },
})
export class AdminMallSpecController extends BaseController {
  @Inject()
  mallSpecService: MallSpecService;

  @Post('/getSpecByGoodsId', { summary: '获得商品规格' })
  async getSpecByGoodsId(@Query('goodsId') goodsId: number) {
    return this.ok(await this.mallSpecService.getSpecByGoodsId(goodsId));
  }
}
